const fs = require('fs');
const path = require('path');

// 读取根目录的VERSION文件
const versionFile = fs.readFileSync(path.resolve(__dirname, '../src/content/VERSION'), 'utf-8');
const DOCS_VERSION = versionFile.match(/DOCS_VERSION=(.*)/)?.[1]?.trim() || '';

// 更新根目录的i18n_website.json
const i18nPath = path.resolve(__dirname, '../src/i18n_website.json');
const i18n = JSON.parse(fs.readFileSync(i18nPath, 'utf-8'));

if (DOCS_VERSION) {
  i18n["version.display"] = `v${DOCS_VERSION}`;
}

fs.writeFileSync(i18nPath, JSON.stringify(i18n, null, 2), 'utf-8');
console.log('根目录 version.display 已更新为:', i18n["version.display"]);

// 更新versions/v13.0目录下的i18n_website.json
const v13VersionPath = path.resolve(__dirname, '../src/versions/v13.0/VERSION');
const v13I18nPath = path.resolve(__dirname, '../src/versions/v13.0/i18n_website.json');

// 检查文件是否存在
if (fs.existsSync(v13VersionPath) && fs.existsSync(v13I18nPath)) {
  try {
    // 读取VERSION.md文件
    const v13VersionFile = fs.readFileSync(v13VersionPath, 'utf-8');
    const v13DocsVersion = v13VersionFile.match(/DOCS_VERSION=(.*)/)?.[1]?.trim() || '';
    
    if (v13DocsVersion) {
      // 读取并更新i18n_website.json
      const v13I18n = JSON.parse(fs.readFileSync(v13I18nPath, 'utf-8'));
      v13I18n["version.display"] = `v${v13DocsVersion}`;
      
      fs.writeFileSync(v13I18nPath, JSON.stringify(v13I18n, null, 2), 'utf-8');
      console.log('v13.0目录 version.display 已更新为:', v13I18n["version.display"]);
    } else {
      console.log('v13.0/VERSION 中未找到 DOCS_VERSION');
    }
  } catch (error) {
    console.error('更新v13.0目录文件时出错:', error.message);
  }
} else {
  console.log('v13.0目录下的VERSION.md或i18n_website.json文件不存在');
}